<?php

namespace app\common\model\logic;

class AdminModel
{
    public function __construct()
    {
        $this->curModel = model('dao.admin');
    }

    public function getListPage($data)
    {
        $page = getPageData(); //获取分页数据
        $where[] = ['id', '>', 0];
        if (isset($data['keyword']) && !empty($data['keyword'])) {
            $where[] = ['name|account|mobile', 'like', '%'.$data['keyword'].'%'];
        }
        $list = $this->curModel->where($where)->limit($page['start'], $page['limit'])->field('id,name,account,email,mobile,head_img,create_time,update_time,status')->order('id desc')->all();
        if ($list->isEmpty()) {
            exception(lang('GET_ERROR'), 0, 'app\\common\\exception\\DataEmptyException');
        } else {
            $data['total'] = $this->curModel->where($where)->count();
            $data['list'] = $list;

            return $data;
        }
    }

    public function saveAdd($data)
    {
        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        $data['status'] = 1;
        $result = $this->curModel->allowField(true)->isUpdate(false)->save($data);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('SAVE_ERROR'), 0, 'app\\common\\exception\\AccountException');
        }
    }

    public function getRow($data)
    {
        $where[] = ['id', '=', $data['id']];

        return $this->getLine($where, 'id,name,account,email,mobile,head_img,create_time,update_time,status');
    }

    //保存编辑
    public function saveEdit($data)
    {
        $this->getRow($data);
        if (isset($data['password'])) {
            unset($data['password']);
        }

        $result = $this->curModel->allowField(true)->isUpdate(true)->save($data);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('SAVE_ERROR'), 0, 'app\\common\\exception\\AccountException');
        }
    }

    //保存删除
    public function saveDel($data)
    {
        $this->getRow($data);
        if ($data['id'] == 1) {
            exception(lang('ROOT_ACCOUT_NOT_DEL'), 0, 'app\\common\\exception\\AccountException');
        }
        $result = $this->curModel->destroy($data['id']);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('DEL_ERROR'), 0, 'app\\common\\exception\\AccountException');
        }
    }

    //获取账号信息
    public function getInfo()
    {
        $tokenRow = model('server.jwt')->getPayload();
        $row = $this->getRow(['id' => $tokenRow->id]);

        return $row;
    }

    //保存账号信息

    public function saveInfo($data)
    {
        $adminRow = $this->getInfo();
        $updateData['id'] = $adminRow->id;
        $updateData['name'] = $data['name'];
        $updateData['email'] = $data['email'];
        $updateData['mobile'] = $data['mobile'];
        if (isset($data['head_img']) && !empty($data['head_img'])) {
            $updateData['head_img'] = $data['head_img'];
        }

        $result = $this->curModel->allowField(true)->isUpdate(true)->save($updateData);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('SAVE_ERROR'), 0, 'app\\common\\exception\\AccountException');
        }
    }

    public function setPassword($data)
    {
        $adminRow = $this->getInfo();
        $updateData['id'] = $adminRow->id;
        $updateData['password'] = password_hash($data['password'], PASSWORD_DEFAULT);

        $result = $this->curModel->allowField(true)->isUpdate(true)->save($updateData);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('SAVE_ERROR'), 0, 'app\\common\\exception\\AccountException');
        }
    }

    public function login($data)
    {
        $where[] = ['account', '=', $data['account']];
        $row = $this->getLine($where, 'id,name,email,head_img,mobile,account,password');
        if (empty($row)) {
            exception(lang('PASSWORD_ERROR').'account', 0, 'app\\common\\exception\\DataEmptyException');
        } else {
            if (password_verify($data['password'], $row['password'])) {
                $token = model('server.jwt')->generateToken($row);
                $this->setToken($row['id'], $token);

                return $token;
            } else {
                exception(lang('PASSWORD_ERROR').'password', 0, 'app\\common\\exception\\AccountException');
            }
        }
    }

    public function getLine($where, $fields = '*')
    {
        return $this->curModel->where($where)->field($fields)->find();
    }

    public function checkToken()
    {
        $row = model('server.jwt')->getPayload();
        $token = $this->getToken($row->id);
        model('server.jwt')->checkToken($token);

        return true;
    }

    public function getPower($data)
    {
        return  model('server.roles_bind')->getAdminBind($data);
    }

    public function savePower($data)
    {
        return  model('server.roles_bind')->saveAdminBind($data);
    }

    /*=====================================================*/
    private function getToken($id)
    {
        $where[] = ['id', '=', $id];
        $row = $this->getLine($where);

        return $row['token'];
    }

    private function setToken($id, $token)
    {
        $updateData['id'] = $id;
        $updateData['token'] = $token;

        return $this->updateRow($updateData);
    }

    private function updateRow($data)
    {
        return $this->curModel->allowField(true)->isUpdate(true)->save($data);
    }
}
